package com.mao.dao.adminmanage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.action.jsonvo.store.JsonVoStore;
import com.mao.action.jsonvo.store.JsonVoUser;
import com.mao.dao.BaseDAOHibernate;
import com.mao.model.store.Store;
import com.mao.model.user.User;
import com.mao.util.Validator;

@Component("adminManageDao")
public class AdminManageDao extends BaseDAOHibernate {
	public List<JsonVoStore> getAllStore(String name) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" From Store s where s.status='" + Store.STATUS_NORMAL + "'");
		if(!Validator.isEmpty(name)){
			hql.append(" and s.companyName like '%" + name + "%'");
		}
		Query query = session.createQuery(hql.toString());
		List<Store> list = query.list();
		List<JsonVoStore> result = new ArrayList<JsonVoStore>();
		JsonVoStore jvs = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Store s : list) {
			jvs = new JsonVoStore();
			jvs.setId(s.getId());
			jvs.setCompanyName(s.getCompanyName());
			jvs.setBossName(s.getBossName());
			jvs.setContractMonth(s.getContractMonth());
			jvs.setAddress(s.getAddress());
			jvs.setStoreEmail(s.getStoreEmail());
			jvs.setStoreMobile(s.getStoreMobile());
			if(s.getCreateTime() != null) {
				jvs.setCreateTime(sdf.format(s.getCreateTime()));
			}
			if(s.getContractTime() != null) {
				jvs.setContractTime(sdf.format(s.getContractTime()));
			}
			jvs.setNote(s.getNote());
			result.add(jvs);
		}
		return result;
	}
	
	public Store getStoreById(long id) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" From Store s where s.id=" + id);
		Query query = session.createQuery(hql.toString());
		List<Store> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	public Integer getAllStoreCount(String name) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("select count(id) From Store s where s.status='" + Store.STATUS_NORMAL + "'");
		if(!Validator.isEmpty(name)){
			hql.append(" and s.companyName like '%" + name + "%'");
		}
		Query query = session.createQuery(hql.toString());
		return ((Long) query.uniqueResult()).intValue();
	}

	public Store saveOrUpdateStore(Store store) {
		this.getHibernateTemplate().saveOrUpdate(store);
		return store;
	}
	
	public List<JsonVoUser> getAllUser(String loginName) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" From User u where u.status='" + User.STATUS_NORMAL + "'");
		if(!Validator.isEmpty(loginName)){
			hql.append(" and u.loginName like '%" + loginName + "%'");
		}
		Query query = session.createQuery(hql.toString());
		List<User> list = query.list();
		List<JsonVoUser> result = new ArrayList<JsonVoUser>();
		JsonVoUser jvu = null;
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (User u : list) {
			jvu = new JsonVoUser();
			jvu.setId(u.getId());
			jvu.setName(u.getName());
			jvu.setLoginName(u.getLoginName());
			jvu.setPassword(u.getPassword());
			jvu.setStoreId(u.getStore().getId());
			jvu.setStoreName(u.getStore().getCompanyName());
			jvu.setPhone(u.getPhone());
			jvu.setAddress(u.getAddress());
			jvu.setIsAdmin(u.getIsAdmin());
			if(u.getParent() != null){
				jvu.setParentId(u.getParent().getId());
			}
			if(u.getCreateTime() != null) {
				jvu.setCreateTime(sdf1.format(u.getCreateTime()));
			}
			if(u.getLoginTime() != null) {
				jvu.setLoginTime(sdf1.format(u.getLoginTime()));
			}
			jvu.setNote(u.getNote());
			result.add(jvu);
		}
		return result;
	}
	
	public Integer getAllUserCount(String loginName) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" select count(id) From User u where u.status='" + User.STATUS_NORMAL + "'");
		if(!Validator.isEmpty(loginName)){
			hql.append(" and u.loginName like '%" + loginName + "%'");
		}
		Query query = session.createQuery(hql.toString());
		return ((Long)query.uniqueResult()).intValue();
	}
	
	public User saveOrUpdateUser(User user) {
		this.getHibernateTemplate().saveOrUpdate(user);
		return user;
	}
	
	public User getUserById(long id) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" From User u where u.id=" + id);
		Query query = session.createQuery(hql.toString());
		List<User> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}
	
	public String valDelStore(long id) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" From User u where u.status = '" + User.STATUS_NORMAL + "' and u.store.id=" + id);
		Query query = session.createQuery(hql.toString());
		List<User> list = query.list();
		if(list.size() > 0){
			return "1";
		}
		return "0";
	}
	
	public List<JsonVoUser> getUserByStore(long storeid) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append(" From User u where u.status='" + User.STATUS_NORMAL + "'");
		if(storeid > 0){
			hql.append(" and u.store.id=" + storeid);
		}
		Query query = session.createQuery(hql.toString());
		List<User> list = query.list();
		List<JsonVoUser> result = new ArrayList<JsonVoUser>();
		JsonVoUser jvu = null;
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (User u : list) {
			jvu = new JsonVoUser();
			jvu.setId(u.getId());
			jvu.setName(u.getName());
			jvu.setLoginName(u.getLoginName());
			jvu.setPassword(u.getPassword());
			jvu.setStoreId(u.getStore().getId());
			jvu.setStoreName(u.getStore().getCompanyName());
			jvu.setPhone(u.getPhone());
			jvu.setAddress(u.getAddress());
			jvu.setIsAdmin(u.getIsAdmin());
			if(u.getParent() != null){
				jvu.setParentId(u.getParent().getId());
			}
			if(u.getCreateTime() != null) {
				jvu.setCreateTime(sdf1.format(u.getCreateTime()));
			}
			if(u.getLoginTime() != null) {
				jvu.setLoginTime(sdf1.format(u.getLoginTime()));
			}
			jvu.setNote(u.getNote());
			result.add(jvu);
		}
		return result;
	}
	
	public Integer getUserByStoreCount(long storeid) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("select count(id) From User u where u.status='" + User.STATUS_NORMAL + "'");
		if(storeid > 0){
			hql.append(" and u.store.id=" + storeid);
		}
		Query query = session.createQuery(hql.toString());
		if(query.list().size() == 0 ){
			return 0;
		}
		return ((Long)query.uniqueResult()).intValue();
	}
}
